﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Mvc;
using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Integration.WebApi;

namespace UserSystem.Web
{
    /// <summary>
    /// 
    /// </summary>
    public class DependencyRegistrar : IDependencyRegistrar
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="builder"></param>
        public void Register(ContainerBuilder builder)
        {
            builder.RegisterType<UserService>().As<IUserService>().InstancePerApiRequest();
            
            builder.RegisterType<EFUnitOfWork>().As<IUnitOfWork>().InstancePerApiRequest();
            
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;
            
            builder.Register<IDbContext>(c => new GenericDbContext(connectionString)).InstancePerApiRequest();

            //reggister mvc and webapi
            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
                   .Where(t =>
                       (typeof (IHttpController).IsAssignableFrom(t) || typeof (IController).IsAssignableFrom(t)) &&
                       t.Name.EndsWith("Controller", StringComparison.Ordinal));

        }
        /// <summary>
        /// 
        /// </summary>
        public int Order
        {
            get { return 1; }
        }
    }

    //public class DependencyRegistrar
    //{
    //    public static void Run()
    //    {
    //        var builder = new ContainerBuilder();
    //        Register(builder);
    //        var container = WebApiIoc(builder);

    //    }

    //    private static void Register(ContainerBuilder builder)
    //    {
    //        builder.RegisterType<UserService>().As<IUserService>().InstancePerApiRequest();
    //        builder.RegisterType<EFUnitOfWork>().As<IUnitOfWork>().InstancePerApiRequest();

    //        string connectionString = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;

    //        builder.Register<IDbContext>(c => new GenericDbContext(connectionString)).InstancePerApiRequest();
           
    //    }

       
    //    private static IContainer WebApiIoc(ContainerBuilder builder)
    //    {
    //        //builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
    //        //       .Where(t =>!t.IsAbstract &&
    //        //           (typeof (IHttpController).IsAssignableFrom(t) || typeof (IController).IsAssignableFrom(t)))
    //        //       .InstancePerLifetimeScope();
    //        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
    //        {
    //            builder.RegisterAssemblyTypes(assembly)
    //                   .Where(t => !t.IsAbstract &&
    //                       (typeof(IHttpController).IsAssignableFrom(t) || typeof(IController).IsAssignableFrom(t)))
    //                   .InstancePerLifetimeScope();
    //        }

    //        //builder.RegisterType<zDit.BusinessCard.SmallSite.Controllers.BlogController>().InstancePerHttpRequest();
    //        //builder.RegisterPersistentConnection(Assembly.GetExecutingAssembly()).InstancePerLifetimeScope();
    //        //builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
    //        var container = builder.Build();
    //        GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
    //        builder.RegisterHttpRequestMessage(GlobalConfiguration.Configuration);
    //        //ApiController　WebApi注入 

    //        //DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); //普通的MVC Controller 注入 



    //        Assembly[] controllerAssemblies = null;
    //        var a =
    //            builder.RegisterAssemblyTypes(controllerAssemblies)
    //                   .Where(t => typeof (IController).IsAssignableFrom(t) &&
    //                               t.Name.EndsWith("Controller", StringComparison.Ordinal));

    //        return container;
    //    }
    //}
}